package com.activitys;

import android.app.Dialog;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bean.PayWxBean;
import com.example.pay.R;
import com.example.pay.databinding.ActivityPaySelectBinding;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.listener.OnClickListener;
import com.presenter.PaySelectPresenter;
import com.views.PaySelectView;

@Route(path = ARouterPath.PATH_PAY_SELECT)
public class PaySelectActivity extends MyBaseFragmentActivity <ActivityPaySelectBinding, PaySelectPresenter> implements PaySelectView {

    private String payTag = "1"; // 1 微信 (默认) 2支付宝

    private Dialog quitDialog;

    @Override
    protected int getLayout() {
        return R.layout.activity_pay_select;
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("确认付款");
        setTopLeftButton(com.kedll.library.R.drawable.ic_arrow_back, new OnClickListener() {
            @Override
            public void onClick(View v) {
                quitDialog.show();
            }
        });
    }

    @Override
    protected PaySelectPresenter getPresenter() {
        return new PaySelectPresenter();
    }


    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        initQuitDialog();

        String money = "￥" + "100.00";
        SpannableString spannableString = new SpannableString(money);
        RelativeSizeSpan span1 = new RelativeSizeSpan(0.8f);
        RelativeSizeSpan span2 = new RelativeSizeSpan(0.6f);
        spannableString.setSpan(span1, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(span2, money.indexOf("."), money.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mBinding.tvMoney.setText(spannableString);


        mBinding.rlWxPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payTag = "1";
                setSelPay(payTag);
            }
        });

        mBinding.rlAliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payTag = "2";
                setSelPay(payTag);
            }
        });

        mBinding.tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARouterPath.PATH_PAY_FINISH)
                        .navigation();
            }
        });
    }

    private void setSelPay(String payTag){
        if (payTag.equals("1")) {
            mBinding.imgWxRadio.setImageDrawable(getResources().getDrawable(R.drawable.pay_radio_sel));
            mBinding.imgAliRadio.setImageDrawable(getResources().getDrawable(R.drawable.pay_radio_no));
        }else if (payTag.equals("2")){
            mBinding.imgWxRadio.setImageDrawable(getResources().getDrawable(R.drawable.pay_radio_no));
            mBinding.imgAliRadio.setImageDrawable(getResources().getDrawable(R.drawable.pay_radio_sel));
        }

    }


    private void initQuitDialog(){
        quitDialog = new Dialog(this);
        View quitView = (View) getLayoutInflater().inflate(R.layout.dialog_pay_back_tips, (ViewGroup) getWindow().getDecorView(),false);
        TextView tvContinue = quitView.findViewById(R.id.tv_continue);
        TextView tvQuit = quitView.findViewById(R.id.tv_quit);

        tvContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quitDialog.dismiss();
            }
        });
        tvQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PaySelectActivity.this.finish();
            }
        });
        quitDialog.setContentView(quitView);
    }

    @Override
    public void onBackPressed() {
        if (!quitDialog.isShowing()) {
            quitDialog.show();
        }
    }

    @Override
    public void payWx(PayWxBean payWxBean, String payType) {

    }
}
